package com.fenghongzhang.day011.wxapi;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.fenghongzhang.day011.MainActivity;
import com.fenghongzhang.day011.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.editorpage.ShareActivity;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.weixin.view.WXCallbackActivity;

public class WXEntryActivity extends WXCallbackActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_w_x_entry);
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList =new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
            ActivityCompat.requestPermissions(this,mPermissionList,123);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    public void share(View view) {
        UMImage image =new UMImage(this,R.mipmap.ic_launcher);//网络图片

        UMWeb web =new UMWeb("http://www.fenghongzhang.com");
        web.setTitle("This is music title");//标题
        web.setThumb(image);//缩略图
        web.setDescription("my description");//描述

        new ShareAction(this).withMedia(web).setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
                .setCallback(shareListener).open();
    }

    private UMShareListener shareListener =new UMShareListener(){
/**
 * @descrption 分享开始的回调
 * @param platform 平台类型
 */
        @Override
        public void onStart(SHARE_MEDIA platform){

        }

/**
 * @descrption 分享成功的回调
 * @param platform 平台类型
 */
        @Override
        public void onResult(SHARE_MEDIA platform){
            Toast.makeText(WXEntryActivity.this,"成功了",Toast.LENGTH_LONG).show();
        }

/**
 * @descrption 分享失败的回调
 * @param platform 平台类型
 * @param t 错误原因
 */
        @Override
        public void onError(SHARE_MEDIA platform,Throwable t){
            Toast.makeText(WXEntryActivity.this,"失败"+t.getMessage(),Toast.LENGTH_LONG).show();
        }

/**
 * @descrption 分享取消的回调
 * @param platform 平台类型
 */
        @Override
        public void onCancel(SHARE_MEDIA platform){
            Toast.makeText(WXEntryActivity.this,"取消了",Toast.LENGTH_LONG).show();

        }
    };
}
